<template>
	<view class="u-wrap">
		<!-- <view class="To" :style="'padding-top:'+ titletop + 'rpx'">
			<view class="tabs">
				<view class="back" @click="backs">
					<img src="../../static/fish/back.png" alt="" />
				</view>
				<view class="tabbs"> 详情 </view>
				<u-popup :height="300" :show="show" @close="close" mode="center" safeAreaInsetTop="true"
					customStyle="position: relative;top:180rpx;color:#000;background:#f7f6f6">
					<view class="container"> </view>
				</u-popup>
			</view>
		</view> -->
		<!-- 内容 -->
		<view class="cnt">
			<view class="cnt_title">
				<view class="cnt_txt" style="text-align: center;">{{ title }}</view>
			</view>
			<view class="cnt_btom__">
				<view class="cnt_btom_" v-for="(item,index) in conts" :key="index">
					<view class="cnt_btom" :class="{'back-text':item.backIndex == '999'}">
						{{ item.content }}
						<img src="/static/home/gx.png" @click="rewrites(item,index)" />
					</view>
				</view>
			</view>
		</view>
		<view class="fixd">
			<view class="fixd_">
				<!-- <view class="fixd_l">字数:{{totalLength}}</view> -->
				<view class="fixd_r_a" @click="copy">
					<image src="/static/fish/pcs.png" mode=""></image>
					<view class="tt">全文复制</view>
				</view>
				<view class="fixd_r_a" @click="save">
					<image src="/static/fish/pcss.png" mode=""></image>
					<view class="tt">全文保存</view>
				</view>
				<view class="fixd_r_b" @click="goaiwriting">
					 <view>全文改写</view>
					  <view class="ts">{{totalLength}}</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		apiMixUrlApi,
		wycUrl
	} from '@/api/assemb.js'
	export default {
		data() {
			return {
				titletop: '',
				titleheight: '',
				show: false,
				contents: [],
				content: [],
				cont: [],
				conts: [],
				values: [],
				title: '',
				backIndex: 998,
				backIndexs: 998,
				backVal: [],
				value: '',
				name: '',
				val: [],
				totalLength: '',
				titles: '',
			}
		},

		onLoad(options) {
			this.getHeight()
			this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
			if (JSON.parse(options.param).name != 'back') {
				// console.log(JSON.parse(options.param))
				let content = JSON.parse(options.param).key
				this.values = content
				this.title = content.title
				//字符串截取问题contents.content

				this.content = content.content.split('},')
				this.content.forEach((item, index) => {
					
					if (this.content.length - 1 !== index) {
						
						let str = item + '}'
						this.contents.push(str)
					} else {
						this.contents.push(item)
					}
					// console.log(index.length,'--------',index.length-1)
				})
				
			} else {
				this.value = JSON.parse(options.param)
				console.log("🚀 ~ file: rewrite.vue:58 ~ onLoad ~ this.value:", this.value.title)
				this.title = this.value.content.value.title
				this.backIndex = this.value.index
				this.backIndexs = this.value.select
				this.name = this.value.name
				//   console.log("🚀 ~ file: rewrite.vue:62 ~ onLoad ~ this.name:", this.name)
				if (this.value.title == undefined) {
					let ad = this.value.values;
					let arr = Object.values(ad);
					this.conts = []
					this.conts = arr;
					this.values = this.conts
				} else {
					this.title = this.value.title
					this.conts = []
					this.conts = this.value.content.value;
					this.values = this.conts
				}
			}
		},
		onShow() {
			this.tap(false)
			this.click()
		},
		computed: {},
		methods: {
			//全文改写 前往伪原创页面
			goaiwriting(){
					
				if(this.isVip || uni.getStorageSync('frequency') > 0){
					if(this.totalLength<4000){
						let params = {
							title: this.titles
						};
						uni.navigateTo({
							url: '/pages_aiTools/aiWriting/aiWriting?param=' + JSON.stringify(params)
						})
					}else{
						uni.showToast({
							title:'字数超过4000,请分段改写',
							icon:'none'
						})
					}
				}else{
					uni.showModal({
						title: '提示',
						content: '免费次数已用完,请开通会员',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								if (uni.getStorageSync('OS') !== 'ios') {
									uni.navigateTo({
										url: '../paymentCenter/paymentCenter'
									});
								} else {
									console.log('00000000')
									uni.navigateTo({
										url: '/pages/webview/view'
									});
								}
							}
						}
					});
				}
			},
			
			getHeight() {
				let res = uni.getMenuButtonBoundingClientRect();
				this.titletop = res.top * 1.37;
				this.titleheight = res.height
				console.log(res);
				console.log("titletop的值", this.titletop);
				console.log("titleheight的值", this.titleheight);
				console.log("一半的titleheight的值", this.titleheight / 2);
			},
			//保存创作文案记录
			saveNab() {
				let today = new Date();
				let month = today.getMonth() + 1;
				let currentDate = today.getFullYear() + "-" + month + "-" + today.getDate();
				let tooles = {
					title: this.titles,
					time: currentDate
				}
				if (uni.getStorageSync('contents')) {
					let contents = uni.getStorageSync('contents')
					contents.push(tooles)
					uni.setStorageSync('contents', contents);
					uni.showToast({
						title: '保存成功',
						icon: "success",
						mask: true,
						duration: 2000
					})
					if (uni.getStorageSync('contents').length > 20) {
						uni.showModal({
							title: '提示',
							content: '文案作品最多存储20条',
							showCancel: false,
							success: function(res) {}
						});
					}
				} else {
					uni.showToast({
						title: '保存成功,请前往我的页面查看',
						icon: "success",
						mask: true,
						duration: 2000
					})
					let contents = []
					contents.push(tooles)
					uni.setStorageSync('contents', contents);
				}
			},
			save() {
				if (this.isVip || uni.getStorageSync('frequency') > 0) {
					console.log('会员 试用次数')
					if (this.isVip) {
						this.saveNab()
					} else {
						this.saveNab()
						let params = {
							consumecoins: 1,
							descinfo: '伪原创消耗',
							consumetype: 1,
						}
						apiMixUrlApi(params, '1043', "ConsumeCoins").then(result => {
							console.log(result, '1043返回值');
							uni.setStorageSync('frequency', result.coins)
						});
					}

				} else {
					uni.showModal({
						title: '提示',
						content: '免费次数已用完,请开通会员',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								if (uni.getStorageSync('OS') !== 'ios') {
									uni.navigateTo({
										url: '../paymentCenter/paymentCenter'
									});
								} else {
									console.log('00000000')
									uni.navigateTo({
										url: '/pages/webview/view'
									});
								}
							}
						}
					});
				}
			},
			copy() {
				console.log('999999999')
				if (this.isVip) {
					uni.setClipboardData({
						data: this.titles,
						showToast: true
					});
				} else if (uni.getStorageSync('frequency') > 0) {
					uni.setClipboardData({
						data: this.titles,
						showToast: true
					});
					let params = {
						consumecoins: 1,
						descinfo: '伪原创消耗',
						consumetype: 1,
					}
					apiMixUrlApi(params, '1043', "ConsumeCoins").then(result => {
						console.log(result, '1043返回值');
						uni.setStorageSync('frequency', result.coins)
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '免费次数已用完,请开通会员',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								if (uni.getStorageSync('OS') !== 'ios') {
									uni.navigateTo({
										url: '../paymentCenter/paymentCenter'
									});
								} else {
									console.log('00000000')
									uni.navigateTo({
										url: '/pages/webview/view'
									});
								}
							}
						}
					});
				}

			},
			tap(e) {
				if (e == false) {
					for (var i = 0; i < this.contents.length; i++) {
						this.cont = this.contents[i]
						this.cont = this.cont.replace('[', '').replace(']', '')
						this.cont = JSON.parse(this.cont.replace(/'/g, '"'))
						//   this.cont['select'] = this.backIndex;
						this.conts.push(this.cont)
					}
				}
			},
			rewrites(item, index) {

				if (this.isVip || uni.getStorageSync('frequency') > 0) {
					if (this.name == 'back') {
						console.log('走第一个')
						let params = {
							content: item.content,
							value: this.values,
							index: index,
							title: this.title
						};
						uni.redirectTo({
							url: '/pages/aitool/Gorewrite?param=' + JSON.stringify(params)
						})
					} else {
						console.log('走第二个')
						let params = {
							content: item.content,
							value: this.values,
							index: index
						};
						uni.navigateTo({
							url: '/pages/aitool/Gorewrite?param=' + JSON.stringify(params)
						})
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '试用次数已用完,请开通会员',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								if (uni.getStorageSync('OS') !== 'ios') {
									uni.navigateTo({
										url: '../paymentCenter/paymentCenter'
									});
								} else {
									console.log('00000000')
									uni.navigateTo({
										url: '/pages/webview/view'
									});
								}
							}
						}
					});
				}


			},
			backs() {
				uni.navigateTo({
					url: '/pages/tools/toolr'
				})
			},
			close() {
				this.level = false;
			},
			//整页面字段
			click() {
				let contents = this.conts.map(e => e.content); // 创建一个新数组，包含所有content的值  
				console.log(contents, '99999999')
				this.titles = contents.toString()
				this.totalLength = contents.reduce((sum, content) => sum + content.length, 0); // 计算所有content的字符数总和  
			},

			sav() {
				let title = {
					title: this.title
				}
				this.conts.push(title)
				uni.setStorageSync('rewrite', this.conts)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
	}

	.banner-img {
		display: flex;
		justify-content: center;
		background: linear-gradient(180deg, #ffeeee 0%, #f8f8f8 100%);
	}

	.tabs {
		// background: linear-gradient(180deg, #ffeeee 0%, #f8f8f8 100%);
		width: 100%;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
	}

	.tabbs {
		//   margin: 30rpx 0 0 41%;
		margin-left: 35%;
		// margin-top: 70rpx;
	}

	.back {
		width: 48rpx;
		height: 48rpx;
		margin-left: 30rpx;
		// margin-top: 70rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.cnt {
		padding: 54rpx;
		// margin-top: 100rpx;
		background: #f5f5f5;
		margin-top: -80rpx;
	}

	.cnt_title {
		width: 408rpx;
		height: 60rpx;
		font-size: 38rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		color: #f1445d;
		margin: 45rpx 118rpx;
	}

	.cnt_txt {
		white-space: nowrap;
		/*不换行*/
		overflow: hidden;
		/*溢出隐藏*/
		text-overflow: ellipsis;
		/*省略号*/
	}

	.cnt_btom__ {
		width: 642rpx;
		font-size: 30rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #332f2f;
		margin-top: 42rpx;
		margin-bottom: 180rpx;
	}

	.cnt_btom_ {
		margin-bottom: 10rpx;
	}

	.cnt_btom {
		text-indent: 2em;

		image {
			width: 143rpx;
			height: 40rpx;
		}
	}

	.back-text {
		color: #50a0fe;
	}

	.fixd {
		width: 750rpx;
		height: 124rpx;
		background: #ffffff;
		box-shadow: 0rpx -6rpx 12rpx 2rpx rgba(0, 0, 0, 0.11);
		position: fixed;
		bottom: 0;
	}

	.fixd_ {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		margin-top: 22rpx;
	}

	.fixd_l {
		width: 204rpx;
		height: 40rpx;
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #828282;
	}

	.fixd_r_a {
		width: 110rpx;
		height: 88rpx;
		// border: 2rpx solid #f1445d;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		.tt{
			font-size: 24rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #332F2F;
		}
		image{
			width: 36rpx;
			height: 36rpx;
		}
	}

	.fixd_r_b {
		width:428rpx;
		height: 88rpx;
		flex-direction: column;
		background: #f1445d;
		border-radius: 40rpx;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
			
		.ts{
			font-size: 22rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #ECECEC;
		}
	}
	.To {
		display: flex;
		// padding-top: 60rpx;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		text-align: center;
		background: linear-gradient(180deg, #ffeeee 0%, #f8f8f8 100%);
		position: fixed;
		width: 100%;
		z-index: 99;
	}
</style>
